/**
 * Created by wlc on 2018/4/18.
 */

/**
 * 修改用户信息
 */
function ChangeUserInfor() {
    var realName = document.getElementById("user-manager-realName").value;
    var university = document.getElementById("user-manager-university").value;
    var department = document.getElementById("user-manager-department").value;
    var email = document.getElementById("user-manager-email").value;
    $.ajax({
        type: 'POST',
        url: '../userManager/saveUserInfo' ,
        data: {realname:realName,university:university,department:department,mail:email},
        success: function (returndata) {
            if (returndata === "true"){
                alert("修改成功！");
                window.location.reload();
            }else {
                alert("修改失败，请重试！")
            }
        },
        error: function (returndata) {
        }
    });
}

/**
 * 修改用户密码
 */
function ChangeUserPassword() {
    var nowPassword = document.getElementById("user-manager-nowPassword").value;
    var newPassword = document.getElementById("user-manager-newPassword").value;
    var confirPassword = document.getElementById("user-manager-confirPassword").value;
    $.ajax({
        type: 'POST',
        url: '../userManager/verifiPass' ,
        data: {password:nowPassword},
        success: function (returndata) {
            if (returndata === "true"){
                if (newPassword === confirPassword){
                    $.ajax({
                        type: 'POST',
                        url: '../userManager/changePass' ,
                        data: {password:newPassword},
                        success: function (returndata) {
                            if (returndata === "true"){
                                alert("修改成功！");
                                window.location.reload();
                            }
                        }
                    })
                }else {
                    alert("确认密码错误，请重试！")
                }
            }else {
                alert("当前密码错误，请重试！")
            }
        }
    });
}

/**
 * 对话框初始化
 */
//data-authority-application
$('#authority-application-modal').on('show.bs.modal', function (event) {
    var button = $(event.relatedTarget);
    var recipient = button.data('whatever');
    var modal = $(this);
    modal.find('.modal-title').text(recipient.split("-")[0]+'数据权限申请');
    var authority_status = document.getElementsByClassName("authority-status")[recipient.split("-")[1]].innerText;
    var authority_input = document.getElementsByClassName("authority-input");
    for (var i = 0; i < authority_input.length; i++){
        if (authority_status.split("")[i] !== "-"){
            authority_input[i].innerHTML = '<span style="background-color: #E4E9E1;color: #009a61;padding: 1px 1px;">已获得</span>';
        }else {
            authority_input[i].innerHTML = '<span class="fa-stack fa-lg"><i class="fa fa-square-o fa-stack-1x authority-select"></i></span>';
        }
    }
});

/**
 * 单击事件监听
 * @param e
 */
document.onclick = function (e) {
    var evt=e||window.event;               //保证兼容性
    var tar=evt.target||evt.srcElement;  //获取触发事件的元素
    //申请权限选择
    if(tar.className.indexOf("authority-select") !== -1){
        var span = tar.parentNode;
        if (span.children.length === 1){
            span.innerHTML += '<i class="fa fa-check fa-stack-1x authority-select" style="color:#6AC241"></i>';
        }else{
            span.children[1].remove();
        }
    }
};

/**
 * 数据权限申请
 */
function dataAuthoritySubmit() {
    var authority_title = document.getElementById("authority-modal-head").innerText;
    var authority_inputs = document.getElementsByClassName("authority-input");
    var authority_application_status = "";
    for (var i = 0; i < authority_inputs.length;i++){
        if (authority_inputs[i].children[0].children.length === 2){
            authority_application_status += "1";
        }else {
            authority_application_status += "0";
        }
    }
    if (authority_application_status === "0000"){
        alert("请选择需要申请的权限！");
    }else {
        $.ajax({
            type: 'POST',
            url: '../messageManager/dataAuthorityApplication' ,
            data: {authorityTitle:authority_title,authorityApplicationStatus:authority_application_status},
            success: function (returndata) {
                if (returndata === "true"){
                    alert("申请成功，等待审核！")
                    window.location.reload();
                }
            }
        });
    }
}